package com.sy.controller;

import com.sy.dto.CoachSubDto;
import com.sy.entity.AdminUser;
import com.sy.entity.Coach;
import com.sy.entity.Member;
import com.sy.exception.BusinessException;
import com.sy.service.CoachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import static com.sy.constant.BusinessConstant.SESSION_VALIDATE_CODE;

@RestController
@RequestMapping("coach")
public class CoachController {
    @Autowired
    private CoachService coachService;

    @RequestMapping("login")
    public Coach coachlogin(HttpSession session, Coach coach, String validateCode) {
        String sessionCode = (String) session.getAttribute(SESSION_VALIDATE_CODE);
        if (!Objects.equals(sessionCode, validateCode)) {
            throw new BusinessException(HttpStatus.FORBIDDEN, "验证码错误,我好烦");
        }
        Coach byLogin = coachService.findByLogin(coach);
        if (byLogin == null) {
            throw new BusinessException(HttpStatus.NOT_FOUND, "账户或密码错误");
        }
        if (byLogin != null) {
            session.setAttribute("loginCoach", byLogin);
        }
        return byLogin;
    }

    @RequestMapping("getinfo")
    public List<Coach> getInfo(HttpSession session) {
        if (session.getAttribute("loginCoach") == null) {
            return null;
        }
        Coach coach = (Coach) session.getAttribute("loginCoach");
        List<Coach> allInfo = coachService.findAllInfo(coach);
        return allInfo;
    }

    @RequestMapping("getallsub")
    public List<CoachSubDto> getAllSubject(HttpSession session) {
        if (session.getAttribute("loginCoach") == null) {
            return null;
        }
        Coach coach = (Coach) session.getAttribute("loginCoach");
        List<CoachSubDto> allSubjectByCoach = coachService.findAllSubjectByCoach(coach);
        System.out.println(allSubjectByCoach);
        return allSubjectByCoach;
    }
    @RequestMapping("update")
    public Boolean updateCoach(Coach coach,HttpSession session) {
        Coach loginCoach=(Coach)session.getAttribute("loginCoach");
        coach.setId(loginCoach.getId());
        return coachService.upDateCoach(coach);
    }
}
